package com.hxzy.tms.action;

import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;

import com.google.gson.Gson;
import com.hxzy.tms.ExtendsActionSupport;
import com.hxzy.tms.SessionHolder;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.UserService;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends ExtendsActionSupport{
	private static final long serialVersionUID = -7604563356079928795L;
	public  static final String SESSION_KEY="SESSION_KEY";
	private String email;
	private String passwd;
	private String remeberMe;
	private UserService userService;
	
	public String index(){
		return INPUT;
	}
	
	public String post() throws Exception{
		User user = userService.login(getEmail(), getPasswd(), getAddr());
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().setContentType("application/json");
		
		if(user != null){
			ActionContext.getContext().getSession().put(SESSION_KEY, user);
			ServletActionContext.getResponse().getWriter().println("{\"status\":1, \"message\":\"登录成功\"}");
			if("1".equals(remeberMe)){
				ServiceResult<String> result = userService.generateSessionHash(getAddr());
				if(result.status == 1){
					addCookie(new Cookie("hash", result.data));
				}
			}
		}else{
			ServletActionContext.getResponse().getWriter().println("{\"status\":0, \"message\":\"账号或者密码错误\"}");
		}
		return null;
	}
	
	public String session() throws Exception{
		User user = (User) ActionContext.getContext().getSession().get("SESSION_KEY");
		if(user ==null){
			user = userService.getUser(getCookie("hash"), getAddr()).data;
		}
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        ServletActionContext.getResponse().setContentType("application/json");
        
        if(user != null){
        	Gson gson = new Gson();
        	ServletActionContext.getResponse().getWriter().println(gson.toJson(user));
        }else{
        	ServletActionContext.getResponse().getWriter().println("{}");
        }
        return null;
	}
	
	public String invalide() throws Exception{
		((SessionMap<String, Object>)ActionContext.getContext().getSession()).invalidate();
		SessionHolder.clear();
		addCookie(new Cookie("hash", null));
		userService.logout();
		
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        ServletActionContext.getResponse().setContentType("application/json");
        ServletActionContext.getResponse().getWriter().println("{\"status\":1, \"message\":\"退出成功\"}");

        return null;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getRemeberMe() {
		return remeberMe;
	}

	public void setRemeberMe(String remeberMe) {
		this.remeberMe = remeberMe;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}